<template>
  <basic-container>
    <avue-crud
      ref="crud"
      :data="data"
      :option="option"
      :page.sync="page"
      :before-open="beforeOpen"
      @on-load="rowLoad"
      @search-change="searchChange"
      @row-update="rowUpdate"
      @row-del="rowDel"
      @row-save="rowSave"
      @size-change="sizeChange"
      @current-change="currentChange"
    >
      <template slot-scope="{ row, type, size }" slot="menu">
        <!-- <el-button :size="size" :type="type" @click="setUser(row)">
          设置用户
        </el-button> -->
        <el-button :size="size" :type="type" @click="onSetMenu(row)"
          >设置权限
        </el-button>
      </template>
    </avue-crud>
    <el-drawer title="权限设置" :visible.sync="menuDrawer">
      <div class="wrap-drawer__content">
        <el-tree
          ref="tree"
          :data="menuData"
          highlight-current
          show-checkbox
          node-key="id"
          :default-expand-all="true"
          :default-checked-keys="menuDataRole"
          :props="menuProps"
        >
        </el-tree>
      </div>
      <span
        class="avue-dialog__footer avue-dialog__footer--right wrap-drawer__footer"
      >
        <el-button @click="$refs.drawer.closeDrawer()">取 消</el-button>
        <el-button type="primary" @click="setRoleMenu"> 确 定 </el-button>
      </span>
    </el-drawer>
    <RoleUser ref="roleUser"></RoleUser>
  </basic-container>
</template>

<script>
import avue from "@/mixins/avue.js";
import { SE } from "@/common/constants.js";
import {
  role_tree,
  role_edit,
  role_delete,
  role_add,
  role_getRoleMenu,
  role_setRoleMenu,
  menu_tree,
} from "@/api/core/permission";
import RoleUser from "./role-user.vue";

export default {
  mixins: [avue],
  components: { RoleUser },
  data() {
    return {
      menuDrawer: false,
      menuData: [],
      menuProps: {
        label: "name",
      },
      menuDataRole: [],
      curRole: {},
      data: [],
      option: {
        dialogWidth: "35%",
        menuType: "menu",
        span: 24,
        column: [
          {
            label: "上级角色",
            prop: "parentId",
            type: "tree",
            // dicUrl: 'api/sys/role/tree',
            // dicData: [],
            props: {
              label: "name",
              value: "id",
            },
            // align: 'left',
            hide: true,
          },
          {
            label: "角色名称",
            prop: "name",
            align: "left",
            search: true,
          },
          {
            label: "角色编码",
            prop: "code",
          },
          {
            label: "启用状态",
            prop: "status",
            type: "radio",
            dicData: SE,
            value: "1",
          },
          {
            label: "角色描述",
            prop: "description",
          },
        ],
      },
    };
  },
  methods: {
    beforeOpen(done, type) {
      // if (['edit', 'add'].includes(type)) {
      //   var crud = this.$refs.crud
      //   role_tree().then((res) => {
      //     crud.updateDic('parentId', this.$s.res(res))
      //   })
      // }
      done();
    },
    setUser(row) {
      this.$refs.roleUser.handleOpen(row);
    },
    onSetMenu(row) {
      this.menuDrawer = true;
      this.curRole = row;
      menu_tree()
        .then((res) => {
          this.menuData = this.$s.res(res);
        })
        .then(() => {
          role_getRoleMenu({ id: row.id }).then((res) => {
            this.menuDataRole = this.$s.res(res);
          });
        });
    },
    // 提交
    setRoleMenu() {
      const nodeSelected = this.$refs.tree.getCheckedNodes(false, true);
      const keys = [];
      nodeSelected.forEach((item) => {
        keys.push(item.id);
      });
      const param = { id: this.curRole.id, menuIds: keys };
      role_setRoleMenu(param).then((res) => {
        this.$s.suc(res);
        this.menuDrawer = false;
        this.menuDataRole = [];
      });
    },
    rowLoad() {
      role_tree(this.params).then((res) => {
        this.data = this.$s.res(res);
        this.$refs.crud.updateDic("parentId", this.data);
      });
    },
    rowSave(row, loading, done) {
      role_add(row)
        .then((res) => {
          this.$s.suc(res);
          this.rowLoad();
          loading();
          done();
        })
        .catch(() => {
          loading();
        });
    },
    rowUpdate(row, index, done, loading) {
      role_edit(row)
        .then((res) => {
          this.$s.suc(res);
          this.rowLoad();
          loading();
          done();
        })
        .catch(() => {
          loading();
        });
    },
    rowDel(row, index, done) {
      this.$confirm("确定数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        role_delete({ ids: row.id }).then((res) => {
          this.$s.suc(res);
          this.rowLoad();
          done();
        });
      });
    },
  },
  mounted() {},
};
</script>

<style lang="less" scoped>
.wrap-drawer__content {
  width: 100%;
  padding: 20px;
  padding-bottom: 100px;
}
.wrap-drawer__footer {
  z-index: 999;
}
</style>
